package com.sai.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sai.common.api.vo.Result;
import com.sai.common.aspect.annotation.PermissionData;
import com.sai.common.system.query.QueryGenerator;
import com.sai.demo.entity.SaiDemo;
import com.sai.demo.service.ISaiDemoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/demo")
@Slf4j
public class DemoController {

    @Autowired
    private ISaiDemoService saiDemoService;

    @GetMapping(value = "/add")
    @RequiresPermissions("demo:add")
    public Result add() {
        return Result.ok();
    }

    @PutMapping(value = "/edit")
    @RequiresPermissions("demo:edit")
    public Result edit() {
        return Result.ok();
    }

    @GetMapping(value = "/query")
    @PermissionData
    public Result query(SaiDemo saiDemo, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                        HttpServletRequest req) {
        Map map = req.getParameterMap();
        QueryWrapper<SaiDemo> queryWrapper = QueryGenerator.initQueryWrapper(saiDemo, req.getParameterMap());
        Page<SaiDemo> page = new Page<SaiDemo>(pageNo, pageSize);

        IPage<SaiDemo> pageList = saiDemoService.page(page, queryWrapper);
        log.info("查询当前页：" + pageList.getCurrent());
        log.info("查询当前页数量：" + pageList.getSize());
        log.info("查询结果数量：" + pageList.getRecords().size());
        log.info("数据总数：" + pageList.getTotal());
        return Result.ok(pageList);
    }

}
